package hu.szyrt.bug.connections;

import org.json.JSONException;
import org.json.JSONObject;

import hu.szyrt.bug.utils.Config;

public class DealConnection extends BaseConnection {

	private String resultText = null;
	
	public DealConnection() {
		super(Config.URL_BUG_DEAL);
		// FIXME: publikáláskor kiszedni
		setTestEmail("sicz.mj@gmail.com");
	}
	
	
	@Override
	public void run() {
		parameterizedURL();
		if(download()){
			// JSON decode
			try {
				JSONObject jsonResult = new JSONObject(rawResults);
				this.resultText = jsonResult.getString("result_text");				
				if(jsonResult.getString("result").equals("YES")){					
					postSuccess();
				}else{ 
					postError();
				}
			} catch (JSONException e) {
				e.printStackTrace();
				postError();
			}
		}else{
			postError();
		}		
	}

	// Setters --------------------------------------
	public void setTestEmail(String email){
		params.put("test", email);
	}
	
	public void setVoucherID(String voucherID){
		params.put("voucher_id", voucherID);
	}
	
	public void setPlaceID(int id){
		params.put("place_id", String.valueOf(id));
	}
	
	public void setMemberID(int id){
		params.put("member_id", String.valueOf(id));
	}
	
	// Getters --------------------------------------
	public String getResultText() {
		return resultText;
	}
}
